package com.ruoyi.email.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.email.domain.TmdEmailRubbish;
import com.ruoyi.email.domain.TmdEmailWrite;
import com.ruoyi.email.service.ITmdEmailDraftService;
import com.ruoyi.email.service.ITmdEmailRubbishService;
import com.ruoyi.email.service.ITmdEmailWriteService;
import com.ruoyi.email.vo.TmdEmailWriteAndSysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/email/draft")
public class TmdEmailDraftController extends BaseController {
    private String prefix = "email/draft";

    @Autowired
    private ITmdEmailWriteService tmdEmailWriteService;
    @Autowired
    private ITmdEmailDraftService tmdEmailDraftService;
    @Autowired
    private ITmdEmailRubbishService tmdEmailRubbishService;
    @RequiresPermissions("email:draft:view")
    @GetMapping()
    public String write()
    {
        return prefix + "/draft";
    }
    /**
     * 查询写信列表
     */
    @RequiresPermissions("email:draft:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TmdEmailWrite tmdEmailWrite)
    {
        startPage();
        List<TmdEmailWriteAndSysUser> list = tmdEmailDraftService.selectTmdEmailWriteList(tmdEmailWrite);
        return getDataTable(list);
    }
    @RequiresPermissions("email:draft:remove")
    @Log(title = "写信", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        String[] strings = Convert.toStrArray(ids);
        for (String eve:strings) {
            int i = Integer.parseInt(eve);
            TmdEmailWrite tmdEmailWrite = new TmdEmailWrite();
            tmdEmailWrite.setId(i);
            tmdEmailWrite.setDraftStatus(0);
            tmdEmailWriteService.updateTmdEmailWrite(tmdEmailWrite);
            TmdEmailRubbish tmdEmailRubbish = new TmdEmailRubbish();
            tmdEmailRubbish.setEmailId(i);
            tmdEmailRubbish.setDraftStatus(1);
            tmdEmailRubbishService.insertTmdEmailRubbish(tmdEmailRubbish);
        }
        return toAjax(1);
    }
    @RequiresPermissions("email:draft:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap, Model model)
    {
        List<SysUser> sysUsers = tmdEmailWriteService.selectUserName();
        model.addAttribute("sysUsers",sysUsers);
        TmdEmailWriteAndSysUser emailWriteAndSysUser = tmdEmailDraftService.selectTmdEmailWriteById(id);
        mmap.put("emailWriteAndSysUser", emailWriteAndSysUser);
        return prefix + "/edit";
    }

    /**
     * 修改保存写信
     */
    @RequiresPermissions("email:draft:edit")
    @Log(title = "写信", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TmdEmailWrite tmdEmailWrite)
    {
        return toAjax(tmdEmailWriteService.updateTmdEmailWrite(tmdEmailWrite));
    }
    @RequiresPermissions("email:draft:submit")
    @Log(title = "写信", businessType = BusinessType.UPDATE)
    @PostMapping("/submit")
    @ResponseBody
    public AjaxResult submit(TmdEmailWrite tmdEmailWrite)
    {
        tmdEmailWrite.setDraftStatus(0);
        tmdEmailWrite.setPostStaus(1);
        tmdEmailWrite.setReceiveStatus(1);
        tmdEmailWrite.setRecivePostTime(DateUtils.getNowDate());
        tmdEmailWriteService.updateTmdEmailWrite(tmdEmailWrite);
        return toAjax(1);
    }
}
